﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using NomCafe;
using NomClasses;
using Cashier;

namespace NomCafe
{
    public partial class frmLogin : DevComponents.DotNetBar.Office2007Form
    {    
        //PUBLIC
        public string dbSchema = "[7nghe_com_nom]";
        public static Main thisMain;
        public static Main_Cashier mainCash = new Main_Cashier();
        public static Main_Manager mainManager = new Main_Manager();        
        
        public frmLogin()
        {
            InitializeComponent();                        
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnSignIn_Click(object sender, EventArgs e)
        {
            login();
        }
        public void login() {            
            string username = txtUsername.Text.Trim();
            string password = txtPassword.Text.Trim();
            if (username == "" || password == "") return;

            btnSignIn.Enabled = false;

            string _strConn = "Data Source=112.213.85.26,2319;Network Library=DBMSSOCN;Initial Catalog=7nghe_db;User ID=7nghe_admin;Password=ava-jPlmpx5YV0;";

            SqlConnection conn = new SqlConnection(_strConn);

            try
            {
                conn.Open();
                try
                {
                    SqlCommand myCommand = new SqlCommand("SELECT * FROM " + dbSchema + ".tblUser WHERE (username=N'" + username + "') AND (password=N'" + password + "')", conn);
                    SqlDataReader myReader = myCommand.ExecuteReader();
                    myReader.Read();

                    // Truyền dữ liệu user qua Form Cashier
                    Lib_Cashier.username = myReader["username"].ToString();
                    Lib_Cashier.password = myReader["password"].ToString();
                    Lib_Cashier.fullname = myReader["fullname"].ToString();
                    Lib_Cashier.cafename = myReader["cafename"].ToString();
                    Lib_Cashier.address = myReader["cafeaddress"].ToString();
                    Lib_Cashier.cafephone = myReader["cafephone"].ToString();
                    Lib_Cashier.cafefax = myReader["cafefax"].ToString();
                    Lib_Cashier.cafelogo = myReader["cafelogo"].ToString();
                    Lib_Cashier.cafetaxcode = myReader["cafetaxcode"].ToString();

                    LoginSuccess(myReader["fullname"].ToString());

                }
                catch (Exception)
                {
                    LoginFailed();
                }
            }
            catch (Exception)
            {
                MessageBox.Show(Language.CONNECTION_FAILED, String.Format(Language.ERROR, Language.TEXT_CONNECT), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            btnSignIn.Enabled = true;
        }
        void LoginSuccess(string fullname)
        {
            try
            {
                MessageBox.Show(String.Format(Language.LOGIN_SUCCESS, fullname));
                if (this.radCashier.Checked)
                    this.CallCashierMain(thisMain);                    
                else
                    this.CallManagerMain(thisMain);
            }
            finally
            {
                this.Close();
            }
        }
        void LoginFailed()
        {
            MessageBox.Show(Language.LOGIN_FAILED, String.Format(Language.ERROR, Language.TEXT_LOGIN), MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        void CallCashierMain(Main main)
        {
            mainCash.MdiParent = main;
            mainCash.ShowDialog();            
        }
        void CallManagerMain(Main main)
        {
            mainManager.MdiParent = main;
            mainManager.ShowDialog();
        }

        private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13 && txtUsername.Text.Trim() !="") login();
        }

    }
}
